﻿Imports System.Xml

Public Class Localizator    

    Public Sub localize(ByVal nodes As XmlNodeList, ByRef doc As XmlDocument, ByVal translate As Boolean)                    
        If translate AndAlso Storage.TranslationLanguages.Length > 0 Then
            For Each Language As String In Storage.TranslationLanguages
                localizeWebSiteMap(nodes, doc, Language)
            Next        
        End If
        localizeWebSiteMap(nodes, doc) 'get the resx file with original values
    End Sub

    Private Sub localizeWebSiteMap(ByVal nodes As XmlNodeList, ByRef doc As XmlDocument, Optional ByVal language As String = "")
        Writer.writeLocalizationHeader(language)
        For Each node As XmlNode In nodes
            Try
                localizeNode(node, doc, language)
            Catch ex As Exception
                Dim i As Integer = 1
            End Try
        Next
        Writer.writeLocalizationFooter()
    End Sub

    Private Sub localizeNode(node As XmlNode, ByRef doc As XmlDocument, ByVal language As String)
        If node.NodeType = XmlNodeType.Element Then
            Dim title As String = node.Attributes("title").Value
            Dim description As String = String.Empty
            If Not node.Attributes("description") Is Nothing Then
                description = node.Attributes("description").Value
            End If

            'the [sep] is for the graphic separator in the menu see:
            'Italian: http://blog.dbtek.it/2012/12/menu-da-websitemap-con-separatori.html
            'English: http://blog-eng.dbtek.it/2012/12/aspnet-menu-from-websitemap-with.html
            Dim resourceKey As String = StrConv(title.Replace("[sep]", ""), VbStrConv.ProperCase).Replace(" ", "")

            'check for translations
            If Not String.IsNullOrEmpty(language) Then                
                title = SitemapTranslator.Translate(title, language)
                If Not String.IsNullOrEmpty(description) Then
                    description = SitemapTranslator.Translate(description, language)
                End If
            End If

            'localization
            Writer.writeLocalization(String.Format("<data name=""{0}.description"" xml:space=""preserve"">", resourceKey))
            Writer.writeLocalization(String.Format("  <value>{0}</value>", description))
            Writer.writeLocalization("</data>")
            Writer.writeLocalization(String.Format("<data name=""{0}.title"" xml:space=""preserve"">", resourceKey))
            Writer.writeLocalization(String.Format("  <value>{0}</value>", title))
            Writer.writeLocalization("</data>")

            'copy with resourceKey
            Dim attr As XmlNode = doc.CreateNode(XmlNodeType.Attribute, "resourceKey", "")
            attr.Value = resourceKey
            node.Attributes.SetNamedItem(attr)

            If node.HasChildNodes Then
                For Each subNode As XmlNode In node.ChildNodes
                    localizeNode(subNode, doc, language)
                Next
            End If
        End If
    End Sub

End Class
